<template>
  <section class="space-control">
    <fragment>
      <div
        class="control-header"
        :class="!showSpaceControl ? 'add-paddimg-bottom' : ''"
        @click.stop="showSpaceControl = !showSpaceControl"
        v-if="showPaddingControl || showMarginControl"
      >
        <span>边距</span>
        <div class="header-action">
          <i
            v-if="spaceData.margin || spaceData.padding"
            class="iconfont icon-xianshi"
            @click.stop="resetMp"
          ></i>
          <i v-else class="iconfont icon-yincang" @click="resetMp"></i>

          <i
            v-if="!showSpaceControl"
            @click.stop="showSpaceControl = !showSpaceControl"
            class="iconfont icon-jiahao"
          ></i>
          <i
            v-else
            @click.stop="showSpaceControl = !showSpaceControl"
            class="iconfont icon-jian"
          ></i>
        </div>
      </div>
      <!-- 间距设置 -->
      <div class="control-body" v-show="showSpaceControl && showMarginControl">
        <i class="iconfont icon-mokuaibianju-xiabianju"></i>
        <a-slider
          class="slider"
          v-model="spaceData.margin"
          :min="0"
          :max="50"
        />
        <div
          @mouseenter="showNumVSet = !showNumVSet"
          @mouseleave="showNumVSet = !showNumVSet"
        >
          <a-input-number
            v-show="showNumVSet"
            class="slider-input"
            :min="0"
            :max="50"
            v-model="spaceData.margin"
          />
          <span class="borderNumSpan" v-show="!showNumVSet">{{
            spaceData.margin || 0
          }}</span>
          <span>px</span>
        </div>
      </div>
      <!-- 两侧边距设置 -->
      <div class="control-body add-top-margin" v-show="showSpaceControl">
        <i class="iconfont icon-mokuaibianju-zuoyoubianju"></i>
        <a-slider
          class="slider"
          v-model="spaceData.padding"
          :min="0"
          :max="50"
        />
        <div
          @mouseenter="showNumHSet = !showNumHSet"
          @mouseleave="showNumHSet = !showNumHSet"
        >
          <a-input-number
            v-show="showNumHSet"
            class="slider-input"
            :min="0"
            :max="50"
            v-model="spaceData.padding"
          />
          <span class="borderNumSpan" v-show="!showNumHSet">{{
            spaceData.padding || 0
          }}</span>
          <span>px</span>
        </div>
      </div>
    </fragment>
    <!-- 圆角设置组件 -->
    <div
      class="control-item border-set"
      :class="showSpaceControl ? 'reduce-padding' : ''"
    >
      <div
        class="control-title flex-between"
        @click.stop="showBorderControl = !showBorderControl"
      >
        <span>圆角</span>
        <div class="header-action">
          <i
            v-if="spaceData.borderRadius"
            class="iconfont icon-xianshi"
            @click.stop="resetSpace('borderRadius')"
          ></i>
          <i
            v-else
            class="iconfont icon-yincang"
            @click.stop="resetSpace('borderRadius')"
          ></i>

          <i
            v-if="!showBorderControl"
            @click.stop="showBorderControl = !showBorderControl"
            class="iconfont icon-jiahao"
          ></i>
          <i
            v-else
            @click.stop="showBorderControl = !showBorderControl"
            class="iconfont icon-jian"
          ></i>
        </div>
      </div>
      <div class="control-content" v-show="showBorderControl">
        <div class="control-body">
          <i class="iconfont icon-mokuaiyuanjiao"></i>
          <a-slider
            class="slider"
            v-model="spaceData.borderRadius"
            :min="0"
            :max="50"
          />
          <div
            @mouseenter="showNumSet = !showNumSet"
            @mouseleave="showNumSet = !showNumSet"
          >
            <a-input-number
              v-show="showNumSet"
              class="slider-input"
              :min="0"
              :max="50"
              v-model="spaceData.borderRadius"
            />
            <span class="borderNumSpan" v-show="!showNumSet">{{
              spaceData.borderRadius || 0
            }}</span>
            <span>px</span>
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
export default {
  name: "spaceControl",
  components: {},
  props: {
    spaceData: {
      type: Object,
      default: {},
    },
    showMarginControl: {
      type: Boolean,
      default: true,
    },
    showPaddingControl: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      showSpaceControl: false,
      showBorderControl: false,
      showNumVSet: false,
      showNumHSet: false,
      showNumSet: false,
    };
  },
  watch: {},
  computed: {},
  methods: {
    // 重置
    resetSpace(type) {
      if (type == "margin" && this.spaceData.margin == 0) return;
      if (type == "padding" && this.spaceData.padding == 0) return;
      if (type == "borderRadius" && this.spaceData.borderRadius == 0) return;
      this.$emit("resetSpace", { type });
    },
    resetMp() {
      if (this.spaceData.margin == 0 && this.spaceData.padding == 0) return;
      this.$emit("resetMp");
    },
  },
};
</script>

<style lang="scss" scoped>
.space-control {
  width: 228px;
  margin: 0 auto;
  .control-header {
    padding: 24px 0 12px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    font-weight: 400;
    line-height: 17px;
    color: #000000;
    .reset {
      color: #8c8c8c;
      cursor: pointer;
      user-select: none;
    }
    span {
      cursor: pointer;
      user-select: none;
    }
  }
  .add-paddimg-bottom {
    padding-bottom: 24px;
  }
  .control-body {
    width: 100%;
    height: 40px;
    background: #f6f7f9;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding-left: 14px;
    .iconfont {
      font-size: 20px;
    }
    .slider {
      margin: 0 16px;
      width: 100px;
      ::v-deep .ant-slider-track {
        background-color: #1890fc;
      }
      ::v-deep .ant-slider-rail {
        background-color: #e6e6e6;
      }
    }
    .slider-input {
      width: 30px;
      font-size: 12px;
      background-color: #f6f7f9;
      ::v-deep .ant-input-number-input {
        padding: 0;
        padding-right: 5px;
        text-align: right;
      }
    }
    span {
      font-size: 12px;
      color: #000000;
    }
    .borderNumSpan {
      width: 30px;
      display: inline-block;
      text-align: right;
      padding-right: 5px;
      border: 1px solid #f6f7f9;
    }
    ::v-deep .ant-input-number-handler-wrap {
      display: none;
    }
  }
  .add-top-margin {
    margin-top: 10px;
  }
  .control-item {
    width: 100%;
    .control-title {
      font-size: 12px;
      color: #000000;
      padding: 24px 0 12px 0;
      text-align: left;
      span {
        cursor: pointer;
        user-select: none;
      }
    }
    .control-content {
      width: 100%;
      .input-section {
        width: 100%;
        height: 40px;
        background: #f6f7f9;
        border-radius: 4px;
        .ant-input {
          border: 0 none;
          height: 100%;
          background-color: #f6f7f9;
        }
      }
    }
  }
  .border-set {
    border-top: 1px solid #f0f2f5;
    .flex-between {
      display: flex;
      justify-content: space-between;
      .reset {
        color: #8c8c8c;
        cursor: pointer;
        user-select: none;
      }
    }
    .control-body {
      width: 100%;
      height: 40px;
      background: #f6f7f9;
      border-radius: 4px;
      display: flex;
      align-items: center;
      padding-left: 14px;
      .iconfont {
        font-size: 20px;
      }
      .slider {
        margin: 0 16px;
        width: 100px;
        ::v-deep .ant-slider-track {
          background-color: #1890fc;
        }
        ::v-deep .ant-slider-rail {
          background-color: #e6e6e6;
        }
      }
      .slider-input {
        width: 30px;
        font-size: 12px;
        background-color: #f6f7f9;
        ::v-deep .ant-input-number-input {
          padding: 0;
          padding-right: 5px;
          text-align: right;
        }
      }
      span {
        font-size: 12px;
        color: #000000;
      }
      .borderNumSpan {
        width: 30px;
        display: inline-block;
        text-align: right;
        padding-right: 5px;
        border: 1px solid #f6f7f9;
      }
      ::v-deep .ant-input-number-handler-wrap {
        display: none;
      }
    }
  }
  .reduce-padding {
    margin-top: 12px;
    padding-top: 24px;
    .control-title {
      padding-top: 0;
    }
  }

  .header-action {
    .iconfont {
      font-size: 16px;
      cursor: pointer;
    }
    .iconfont:first-child {
      margin-right: 5px;
    }
  }
}
</style>
